import LTable from "@/components/public/lTable/lTable";
import useNavigator from "@/hooks/useNavigator";
import { computed, defineComponent, reactive, ref } from "vue";
import discountListAddService from "@/modules/channelManagement/channelUsersPage/discountSetPage/discountListAdd/discountListAdd.service";
import styles from "./add.module.less";

export default defineComponent({
  emits: ['cancel', 'confirm'],
  setup(props, { emit }) {
    const navigator = useNavigator();
    const state = reactive({
      toolbar: {
        left: [
          "refresh",
        ],
        right: ["filter"],
      },
      dataSource: [],
      columns: [
        {
          title: "产品ID",
          dataIndex: "insurance_id",
          key: "id",
          align: "center",
          width: "50px",
        },
        {
          title: "产品名称",
          dataIndex: "name",
          align: "center",
          width: "200px",
        },
      ],
      loading: false,
      query: {
        user_id: navigator.route?.query?.id || '',
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
      insurance_id: [],
      rate: '',
    } as any);
    // 选中项变更
    const onSelectChange = (changableRowKeys: any[]) => {
      state.insurance_id = changableRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        selectedRowKeys: state.insurance_id,
        fixed: true,
        onChange: onSelectChange,
      };
    });
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    const $service = new discountListAddService();
    const LTableRef = ref(null as any);
    return () => (
      <div style={{ width: "100%", padding: "15px" }}>
        <LTable
          ref={LTableRef}
          service={$service}
          toolbar={state.toolbar}
          v-model={state}
        >
          <a-table
            bordered={true}
            scroll={{ x: "max-content" }}
            dataSource={state.dataSource}
            columns={state.columns}
            pagination={state.pagination}
            loading={state.loading}
            onChange={tableChangeHandler}
            row-selection={rowSelection.value}
            rowKey="insurance_id"
          />
        </LTable>
        <div class={styles.rateBox}>
          <span class={styles.title}>折扣率：</span>
          <a-input-number v-model:value={state.rate} class={styles['string-input']} max={0.3} min={0} />
          <span class={styles.tips}>（折扣率，不能超过0.3且小于1）</span>
        </div>
        <div class={styles.btns}>
          <a-button onClick={() => { emit('cancel') }}>取消</a-button>
          <a-button type="primary" onClick={() => { emit('confirm', state) }}>确定</a-button>
        </div>
      </div>
    )
  }
})